function reverse(motor) 
  forward(motor, -speedReading(motor))
end

function oneWay(distance)
  resetRotationCount(A)
  reverse(A)
  reverse(B)
  repeat until activeLightReading(3) < 37 or abs(rotationCounts(A)) > distance
end

function twoWay()
  distance = 10
  repeat
    oneWay(distance)
    distance = distance * 2
  until activeLightReading(3) < 37
end

repeat
  light = activeLightReading(3)
  if light > 37 then
    forward(A)
    forward(B)
  else 
    twoWay()
  end
until isPressed(ESCAPE)

